package com.qqtech.qquan.mall.controller;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.util.PinyingUtil;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.mall.enums.MallBrandStatusDictEnum;
import com.qqtech.qquan.mall.enums.MallCategoryStatusDictEnum;
import com.qqtech.qquan.mall.model.MallBrand;
import com.qqtech.qquan.mall.model.MallBrandQuery;
import com.qqtech.qquan.mall.model.MallBrandVo;
import com.qqtech.qquan.mall.model.MallCategoryVo;
import com.qqtech.qquan.mall.service.MallBrandService;
import com.qqtech.qquan.mall.service.MallCategoryService;
import com.qqtech.qquan.sys.controller.BaseAdminController;

@Controller
@RequestMapping(value = "/mall/brand")
public class MallBrandController extends BaseAdminController {
	@Resource
	private MallBrandService mallBrandService;

	@Resource
	private MallCategoryService mallCategoryService;

	@Override
	protected BaseService<MallBrand> getBaseService() {
		return mallBrandService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		List<MallCategoryVo> mallCategoryVoList = mallCategoryService
				.queryFirstLevelList(MallCategoryStatusDictEnum.VALID.getCode());
		model.addAttribute("mallCategoryVoList", mallCategoryVoList);
		return "/mall/brand-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(MallBrandQuery query, HttpServletRequest request,
			HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(),
				new PageOrder(Direction.DESC, "id"));
		Page<MallBrandVo> page = mallBrandService.queryPageListAdmin(query,
				pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Model model) {
		List<MallCategoryVo> mallCategoryVoList = mallCategoryService
				.queryFirstLevelList(MallCategoryStatusDictEnum.VALID.getCode());
		model.addAttribute("mallCategoryVoList", mallCategoryVoList);
		return "/mall/brand-update";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Result doAdd(MallBrandVo mallBrandVo, HttpServletRequest request,
			HttpServletResponse response) {
		Result ret = new Result();
		if (mallBrandVo == null) {
			ret.addErr("参数不能为空");
		} else {
			if (StringUtils.isBlank(mallBrandVo.getName())) {
				ret.addErr("请输入品牌名称");
			}
			if (mallBrandVo.getStatus() == null) {
				ret.addErr("状态不能为空");
			}
			if (mallBrandVo.getCategoryId() == null) {
				ret.addErr("分类不能为空");
			}
		}
		if (ret.checkForm()) {
			Timestamp now = TimeUtil.now();
			MallBrand mallBrand = new MallBrand();
			mallBrand.setName(mallBrandVo.getName().trim());
			mallBrand.setStatus(mallBrandVo.getStatus());
			mallBrand.setLogo(""); // 暂时为空
			mallBrand.setNewAt(now);
			mallBrand.setUpdAt(now);
			mallBrand.setCategoryId(mallBrandVo.getCategoryId());
			mallBrand.setFirstLetter(PinyingUtil.getPinYin(mallBrand.getName().substring(0,1),true));
			mallBrandService.insert(mallBrand);
		}
		return ret;
	}
	
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response) {
		MallBrandVo mallBrandVo = mallBrandService.getById(id);
		if (null != mallBrandVo && null != mallBrandVo.getCategoryId()) {
			/*MallCategoryVo categoryVo = mallCategoryService.getById(mallBrandVo.getCategoryId());
			mallBrandVo.setCategoryName(categoryVo.getName());*/
			// 查询他的所有上级分类
			List<MallCategoryVo> allParentCategory = mallCategoryService.findAllParent(mallBrandVo.getCategoryId());
			request.setAttribute("allParentCategory", allParentCategory);
			List<MallCategoryVo> mallCategoryVoList = mallCategoryService
					.queryFirstLevelList(MallCategoryStatusDictEnum.VALID.getCode());
			request.setAttribute("mallCategoryVoList", mallCategoryVoList);
		}
		request.setAttribute("mallBrandVo", mallBrandVo);
		return "/mall/brand-update";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Result doUpdate(MallBrandVo mallBrandVo) {
		Result ret = new Result();
		if (mallBrandVo == null || mallBrandVo.getId() == null) {
			ret.addErr("参数不能为空");
		} else {
			if (StringUtil.isBlank(mallBrandVo.getName())) {
				ret.addErr("请输入品牌名称");
			}
			if (mallBrandVo.getStatus() == null) {
				ret.addErr("状态不能为空");
			}
		}
		if (ret.checkForm()) {
			MallBrand update = new MallBrand();
			update.setId(mallBrandVo.getId());
			update.setName(mallBrandVo.getName().trim());
			update.setStatus(mallBrandVo.getStatus());
			update.setUpdAt(TimeUtil.now());
			if (mallBrandVo.getCategoryId() != null) {
				update.setCategoryId(mallBrandVo.getCategoryId());
			}
			update.setFirstLetter(PinyingUtil.getPinYin(update.getName().substring(0,1),true));
			mallBrandService.updateById(update);
		}
		return ret;
	}

	@RequestMapping(value = "/getByCategoryId")
	@ResponseBody
	public Result getByCategoryId(String categoryId) {
		Result result = new Result();
		if (categoryId != null) {
			MallBrandQuery q = new MallBrandQuery();
			q.setCategoryId(categoryId);
			q.setStatus(MallBrandStatusDictEnum.NORMAL.getCode());//有效的
			Pageable pageKit = new PageKit(q.getPage(), 9999,
					new PageOrder(Direction.ASC, "firstLetter"));
			List<MallBrandVo> list = mallBrandService.queryList(q, pageKit);
			result.put("brandList", list);
		}
		return result;
	}
}
